/*
 * main.h
 *
 *  Created on: Nov 11, 2013
 *      Author: Ahmed
 */

#include "stm32f10x.h"

#ifndef MAIN_H_
#define MAIN_H_

#define ISR_NONE						  0b0	// ISR defined values are selected to be ORed together to handle
#define ISR_RTC_SEC				          0b1	// One second has elapsed
#define ISR_RTC_ALARM				     0b10	// The RTC set alarm has expired
#define ISR_EXTI12				        0b100
#define ISR_EXTI13				       0b1000
#define ISR_EXTI14				      0b10000
#define ISR_EXTI15				     0b100000
#define ISR_USART_TX			    0b1000000
#define ISR_USART_RX			   0b10000000
#define ISR_SPI					  0b100000000
#define ISR_TIM2_BT_SAMPLE		 0b1000000000
#define ISR_TIM2_LCD_BLINK		0b10000000000

#define bool _Bool
#define FALSE 0
#define TRUE !FALSE
#define	LOG_HEADER_ID		0b01010101		// Dec 85
#define	USER_SETTINGS_ID	0b10101010		// Dec 170

#define	FREQ_UNIT_MINUTES	0
#define	FREQ_UNIT_HOURS		1
#define	FREQ_UNIT_DAYS		2

#define	ENABLED		TRUE
#define	DISABLED	FALSE
#define	APPEND		TRUE
#define	OVERWRITE	FALSE

#define EEPROM_SIZE		32768		// We have 32KB of EEPROM
#define	START_OF_TIME	1388768400	// define the start of time being 10/12/2013 01:00

#define	PA1_INPUT	0				// DHT22 sensor
#define	PA1_OUTPUT	1

typedef	struct						// data record stored on eeprom for each reading
{
	  uint8_t temperature;
	  uint8_t humidity;
} dataRecDef;

typedef	struct						// data record stored on eeprom for each reading
{
	  uint8_t  separator;			// 0b01010101 (Dec 85) identify the start of a new log
	  uint32_t startLogTime;
	  uint32_t stopLogTime;
	  uint32_t RTC_Alarm_Count;
} LogHeaderDef;

// User settings
typedef struct						// data record stored on eeprom for each reading
	{
	  uint8_t	ID;					// use it to tell if the userSetting was stored on EEPROM or not
	  uint32_t	startLogTime;		// Time stamp for when logging should start or current date and time if we should have started
	  uint32_t	stopLogTime;		// Time stamp for when logging should stop or stopped.
	  uint8_t 	freq_type;			// minutes (0), hours (1), days (2)
	  uint8_t 	freq_val;			// value being 1-60, 1-24, 1-30
	  uint8_t 	log_update_mode;	// Log from start or append to last log
	  uint16_t	eepromWriteAddr;	// Hold the last written address when logging has stopped.
	  	  	  	  	  	  	  	  	// Needed to resume logging in log append mode
	  uint8_t 	LCD_timeout;		// LCD to turn off after 15/30 secs
	  uint8_t 	LCD_light_timeout;	// LCD Light turns off after 5/10/15 secs
	} userSettingsDef;

void TimingDelay_Decrement(void);
void Delay(uint32_t);
void RTC_ALR_Interrupt(uint8_t);
uint8_t	sizeof_dataRecStruct(void);
uint8_t	sizeof_userSettingsStruct(void);
uint8_t	sizeof_logHeaderStruct(void);
uint32_t Calc_RTC_Alarm_Count(uint8_t, uint8_t);

#endif /* MAIN_H_ */
